package search;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;

import ajaxmethods.SessionMethod;


public class MapSearchPage extends SessionMethod {

	@Override
	public void perform(HttpServletRequest request, HttpServletResponse response)
	{
		super.perform(request, response);
		 try {
			 StringBuffer sb = new StringBuffer();
			 sb.append("<div id=\"mapsearchWrapper\">");
			 //sb.append("<div id=\"searchfieldControl\"><a href=\"javascript:void(0);\" onclick=\"toggleSidePanel(this)\"></a></div>");
			 sb.append("<div id=\"searchfieldControl\"><div style=\"padding:5px;\"><div style=\"width:50px;display:block;text-align:center\" id=\"detailview\"><a class=\"detailLink\" onclick=\"toggleSidePanel(this)\">hide</a></div></div></div>");
			 //&larr;
			 
			 sb.append("<div id=\"mainContent\">" +
			 		"<div class=\"sidepanel\">" +
			 		"<input type=\"hidden\" id=\"viewtype\" name=\"viewtype\" value=\"map\">" +
			 		"<div><h5 class=\"label\">Zip Code:</h5><br><input type=\"text\" name=\"zip\"></div>" +
			 		"<div><h5 class=\"label\">Property Type:</h5><br><select name=\"useCode\">" +
			 		"<option value=\"Single Family\">Single Family</option>" +
			 		"<option value=\"Multi Family\">Multi Family</option>" +
			 		"<option value=\"Condo\">Condo</option>" +
			 		"<option value=\"Vacant Land\">Vacant Land</option>" +
			 		"</select></div>" +
			 		"<div id=\"pricefields\">" +
					"<h5 class=\"label\">Price:</h5><br>" +
					"<h6>MIN:&nbsp;</h6><input name=\"minprice\" type=\"text\"><h5 class=\"label\">,000</h5>" +
					"<h6>&nbsp;&nbsp;MAX:&nbsp;</h6><input name=\"maxprice\" type=\"text\"><h5 class=\"label\">,000</h5>" +
					"</div>" +
			 		"<div id=\"chainFields\"><div style=\"float:left;padding-right:16px;\">" +
					"<h5 class=\"label\">Beds:</h5><br><select name=\"bedrooms\"><option value=\"\"></option><option value=\"1\">1+</option>" +
					"<option value=\"2\">2+</option><option value=\"3\">3+</option><option value=\"4\">4+</option><option value=\"5\">5+</option><option value=\"6\">6+</option>" +
					"<option value=\"7\">7+</option><option value=\"8\">8+</option></select></div>" +
					"<div><h5 class=\"label\">Baths:</h5><br><select name=\"bathrooms\"><option value=\"\"></option><option value=\"1\">1+</option>" +
					"<option value=\"2\">2+</option><option value=\"3\">3+</option><option value=\"4\">4+</option>" +
					"<option value=\"5\">5+</option></select></div>" +
			 		"<div id=\"sqft\">" +
			 		"<h5 class=\"label\">Sq. Ft</h5><br><select name=\"finishedSQFT\" style=\"font-size:10px;\"><option value=\"\">Any</option><option value=\"500\">500+</option><option value=\"600\">600+</option><option value=\"700\">700+</option><option value=\"800\">800+</option><option value=\"900\">900+</option><option value=\"1000\">1000+</option><option value=\"1250\">1250+</option><option value=\"1500\">1500+</option><option value=\"2000\">2000+</option><option value=\"3000\">3000+</option><option value=\"5000\">5000+</option><option value=\"10000\">10000+</option></select></div>" +
			 		"<div><button class=\"esobutton\" onclick=\"doPostWithInputs('search.PerformMapSearch','input,select')\" id=\"searchlistings\" title=\"Search Listings\" type=\"submit\">Find Listings</button></div>" +
			 		"<div><button style=\"padding: 4px 30px;\" class=\"esobutton\" onclick=\"doPostWithInputs('search.SaveSearch','input,select')\" id=\"saveSearch\" title=\"Search Listings\" type=\"submit\">Save Search</button></div>" +
			 		"</div></div>");
			 sb.append("<div class=\"relative\"><div class=\"viewtabs\"><div class=\"tab selected\"><a class=\"mapview\" onclick=\"toggleListView(this)\">Map View</a></div><div class=\"tab\">" +
			 		"<a class=\"listview\" onclick=\"toggleListView(this)\">List View</a></div></div></div>");
			 sb.append("<div id=\"map_canvas\"></div>");
			 sb.append("<div id=\"list_view\" style=\"display:none;width: 96%;margin:0px 20px;\">" +
			 		"<div class=\"relativeWrapper\"><div class=\"message\">Enter criteria, then click 'Find Listings' to view results.</div></div>" +
			 		"</div>");
			 sb.append("</div></div>");
			
			 data.put("map",sb.toString()).put("mapjs", javascriptOut(request).toString()).write(response.getWriter());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private StringBuffer javascriptOut(HttpServletRequest request) {
		StringBuffer sb = new StringBuffer();
		sb.append("<script>");
		sb.append("function toggleListView(elem) {\n"+
				"if($(elem).hasClass('mapview') && !$(elem).parent().hasClass('selected')) {" +
				"	$('#list_view').slideToggle(function(){ $('#map_canvas').slideToggle(function(){MapJS.reset();});});\n" +
				"	$('#viewtype').val('map');" +
				"	$('.viewtabs .tab').each(function(){if($(this).hasClass('selected')) $(this).removeClass('selected'); else $(this).addClass('selected'); });\n" +
				"}" +
				"else if(!$(elem).parent().hasClass('selected'))" +
				"{" +
				"	$('#viewtype').val('list');" +
				"	$('#mainContent .sidepanel').height(635);\n" +
				"	$('#map_canvas').slideToggle(function(){ $('#list_view').slideToggle();});\n" +
				"	$('.viewtabs .tab').each(function(){if($(this).hasClass('selected')) $(this).removeClass('selected'); else $(this).addClass('selected'); });\n" +
				"}" +
				"}");
		if(request.getParameterMap().containsKey("z") && request.getParameter("z")!=null)
		{
			String zip = request.getParameter("z");
			sb.append("$(document).ready(function(){" +
					"$('input[name=\"zip\"]').val("+zip+");" +
					"doPostWithInputs('search.PerformMapSearch','input,select')" +
					"});");
		}
		sb.append("</script>");
		return sb;
	}
}
